package com.example.wangyuyuan.androidframe;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.wangyuyuan.androidframe.manager.FragmentFactory;
import com.example.wangyuyuan.androidframe.mvp.base.BaseActivity;
import com.example.wangyuyuan.androidframe.mvp.base.BaseFragment;
import com.example.wangyuyuan.androidframe.mvp.base.CommonFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    private List<BaseFragment> mFragmentList = new ArrayList<>(4);

    @BindView(R.id.main_viewpager)
    ViewPager main_viewpager;

    //    底部
    @BindView(R.id.rlMessage)
    LinearLayout rlMessage;
    @BindView(R.id.tvMessageNormal)
    TextView tvMessageNormal;
    @BindView(R.id.tvMessagePress)
    TextView tvMessagePress;
    @BindView(R.id.tvMessageCount)
    TextView tvMessageCount;
    @BindView(R.id.tvMessageTextNormal)
    TextView tvMessageTextNormal;
    @BindView(R.id.tvMessageTextPress)
    TextView tvMessageTextPress;

    @BindView(R.id.rlFriend)
    LinearLayout rlFriend;
    @BindView(R.id.tvFriendNormal)
    TextView tvFriendNormal;
    @BindView(R.id.tvFriendPress)
    TextView tvFriendPress;
    @BindView(R.id.tvFriendTextNormal)
    TextView tvFriendTextNormal;
    @BindView(R.id.tvFriendTextPress)
    TextView tvFriendTextPress;
    @BindView(R.id.tvFriendCount)
    TextView tvFriendCount;
    @BindView(R.id.tvFriendRedDot)
    TextView tvFriendRedDot;

    @BindView(R.id.rlFavourite)
    LinearLayout rlFavourite;
    @BindView(R.id.tvFavouriteNormal)
    TextView tvFavouriteNormal;
    @BindView(R.id.tvFavouritePress)
    TextView tvFavouritePress;
    @BindView(R.id.tvFavouriteTextNormal)
    TextView tvFavouriteTextNormal;
    @BindView(R.id.tvFavouriteTextPress)
    TextView tvFavouriteTextPress;
    @BindView(R.id.tvFraviouteyCount)
    TextView tvFraviouteyCount;

    @BindView(R.id.rlMine)
    LinearLayout rlMine;
    @BindView(R.id.tvMineNormal)
    TextView tvMineNormal;
    @BindView(R.id.tvMinePress)
    TextView tvMinePress;
    @BindView(R.id.tvMineTextNormal)
    TextView tvMineTextNormal;
    @BindView(R.id.tvMineTextPress)
    TextView tvMineTextPress;
    @BindView(R.id.tvMineCount)
    TextView tvMineCount;


    private void setTransparency() {
        tvMessageNormal.getBackground().setAlpha(255);
        tvFriendNormal.getBackground().setAlpha(255);
        tvFavouriteNormal.getBackground().setAlpha(255);
        tvMineNormal.getBackground().setAlpha(255);
        tvMessagePress.getBackground().setAlpha(1);
        tvFriendPress.getBackground().setAlpha(1);
        tvFavouritePress.getBackground().setAlpha(1);
        tvMinePress.getBackground().setAlpha(1);
        tvMessageTextNormal.setTextColor(Color.argb(255, 153, 153, 153));
        tvFriendTextNormal.setTextColor(Color.argb(255, 153, 153, 153));
        tvFavouriteTextNormal.setTextColor(Color.argb(255, 153, 153, 153));
        tvMineTextNormal.setTextColor(Color.argb(255, 153, 153, 153));
        tvMessageTextPress.setTextColor(Color.argb(0, 69, 192, 26));
        tvFriendTextPress.setTextColor(Color.argb(0, 69, 192, 26));
        tvFavouriteTextPress.setTextColor(Color.argb(0, 69, 192, 26));
        tvMineTextPress.setTextColor(Color.argb(0, 69, 192, 26));
    }

    @Override
    protected void processLogic() {

    }

    @Override
    protected void setListener() {
        main_viewpager.setOnPageChangeListener(this);
        rlMessage.setOnClickListener(this);
        rlFriend.setOnClickListener(this);
        rlFavourite.setOnClickListener(this);
        rlMine.setOnClickListener(this);
    }

    @Override
    protected void findViewById() {
        //默认选中第一个
        setTransparency();
        tvMessagePress.getBackground().setAlpha(255);
        tvMessageTextPress.setTextColor(Color.argb(255, 69, 192, 26));
        mFragmentList.add(FragmentFactory.getInstance().getChatFragment());
        mFragmentList.add(FragmentFactory.getInstance().getFriendFragment());
        mFragmentList.add(FragmentFactory.getInstance().getFavouriteFragment());
        mFragmentList.add(FragmentFactory.getInstance().getMineFragment());
        //设置ViewPager的最大缓存页面
        main_viewpager.setOffscreenPageLimit(3);
        main_viewpager.setAdapter(new CommonFragmentPagerAdapter(getSupportFragmentManager(), mFragmentList));

    }

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    public void onClick(View v) {
        setTransparency();
        switch (v.getId()) {
            case R.id.rlMessage:
                main_viewpager.setCurrentItem(0, false);
                tvMessagePress.getBackground().setAlpha(255);
                tvMessageTextPress.setTextColor(Color.argb(255, 69, 192, 26));
                break;
            case R.id.rlFriend:
                main_viewpager.setCurrentItem(1, false);
                tvFriendPress.getBackground().setAlpha(255);
                tvFriendTextPress.setTextColor(Color.argb(255, 69, 192, 26));
                break;
            case R.id.rlFavourite:
                main_viewpager.setCurrentItem(2, false);
                tvFavouritePress.getBackground().setAlpha(255);
                tvFavouriteTextPress.setTextColor(Color.argb(255, 69, 192, 26));
                break;
            case R.id.rlMine:
                main_viewpager.setCurrentItem(3, false);
                tvMinePress.getBackground().setAlpha(255);
                tvMineTextPress.setTextColor(Color.argb(255, 69, 192, 26));
                break;
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //根据ViewPager滑动位置更改透明度
        int diaphaneity_one = (int) (255 * positionOffset);
        int diaphaneity_two = (int) (255 * (1 - positionOffset));
        switch (position) {
            case 0:
                tvMessageNormal.getBackground().setAlpha(diaphaneity_one);
                tvMessagePress.getBackground().setAlpha(diaphaneity_two);
                tvFriendNormal.getBackground().setAlpha(diaphaneity_two);
                tvFriendPress.getBackground().setAlpha(diaphaneity_one);
                tvMessageTextNormal.setTextColor(Color.argb(diaphaneity_one, 153, 153, 153));
                tvMessageTextPress.setTextColor(Color.argb(diaphaneity_two, 69, 192, 26));
                tvFriendTextNormal.setTextColor(Color.argb(diaphaneity_two, 153, 153, 153));
                tvFriendTextPress.setTextColor(Color.argb(diaphaneity_one, 69, 192, 26));
                break;
            case 1:
                tvFriendNormal.getBackground().setAlpha(diaphaneity_one);
                tvFriendPress.getBackground().setAlpha(diaphaneity_two);
                tvFavouriteNormal.getBackground().setAlpha(diaphaneity_two);
                tvFavouritePress.getBackground().setAlpha(diaphaneity_one);
                tvFriendTextNormal.setTextColor(Color.argb(diaphaneity_one, 153, 153, 153));
                tvFriendTextPress.setTextColor(Color.argb(diaphaneity_two, 69, 192, 26));
                tvFavouriteTextNormal.setTextColor(Color.argb(diaphaneity_two, 153, 153, 153));
                tvFavouriteTextPress.setTextColor(Color.argb(diaphaneity_one, 69, 192, 26));
                break;
            case 2:
                tvFavouriteNormal.getBackground().setAlpha(diaphaneity_one);
                tvFavouritePress.getBackground().setAlpha(diaphaneity_two);
                tvMineNormal.getBackground().setAlpha(diaphaneity_two);
                tvMinePress.getBackground().setAlpha(diaphaneity_one);
                tvFavouriteTextNormal.setTextColor(Color.argb(diaphaneity_one, 153, 153, 153));
                tvFavouriteTextPress.setTextColor(Color.argb(diaphaneity_two, 69, 192, 26));
                tvMineTextNormal.setTextColor(Color.argb(diaphaneity_two, 153, 153, 153));
                tvMineTextPress.setTextColor(Color.argb(diaphaneity_one, 69, 192, 26));
                break;
        }

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
